﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using IOA.MES.Client.Common;

namespace IOA.MES.Client.FinalInspection
{
    public delegate string GetImageDelegate();

    /// <summary>
    /// WinELImage.xaml 的交互逻辑
    /// </summary>
    public partial class WinELImage : Window
    {
        private string elImage;
        private GetImageDelegate getImageDelegate;
        public WinELImage(GetImageDelegate del)
        {
            InitializeComponent();
            this.getImageDelegate = del;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            elImage = getImageDelegate();
            if (!string.IsNullOrWhiteSpace(elImage) && File.Exists(elImage))
            {
                SetImageSource();
            }
        }
        public void SetImage(string image)
        {
            if (!string.IsNullOrWhiteSpace(image) && File.Exists(image))
            {
                elImage = image;
                SetImageSource();
            }
        }
        private void SetImageSource()
        {
            var bmi = new BitmapImage();
            bmi.BeginInit();
            bmi.StreamSource = new MemoryStream(File.ReadAllBytes(elImage));
            if (bmi.StreamSource == null)
            {
                CommonMessage.Alert(this, "EL2图片不存在，请重新过EL2\r\n");
                return;
            }
            else
            {
                bmi.EndInit();
                imgEl.Source = bmi;
            }
        }

        private void Window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                this.DialogResult = true;
                this.Close();
            }
        }
    }
}
